package com.general.client.widgets.modalwindow;

import java.util.Iterator;

import com.general.client.basewidget.BaseWidget;
import com.general.client.widgets.taperedbox.TaperedBox;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.InsertPanel;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Widget;

public class ModalWindow extends BaseWidget implements InsertPanel, HasWidgets {

	@UiField
	FlowPanel container;

	@UiField
	PopupPanel popup;

	@UiField
	TaperedBox taperedBox;

	@UiField
	Anchor xButton;

	private static ModalWindowUiBinder uiBinder = GWT
			.create(ModalWindowUiBinder.class);

	interface ModalWindowUiBinder extends UiBinder<Widget, ModalWindow> {
	}

	public ModalWindow() {
		initWidget(uiBinder.createAndBindUi(this));
		container.setVisible(false);
		popup.setAnimationEnabled(true);
		popup.setAutoHideEnabled(true);
		popup.setGlassEnabled(true);
		popup.setModal(true);

		xButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				show(false);
			}
		});
	}

	public void show(boolean show) {
		if (show) {
			container.setVisible(true);
			popup.center();
			popup.show();
		} else {
			popup.hide();
			container.setVisible(false);
		}
	}

	@Override
	public Widget getWidget(int index) {
		return taperedBox.getWidget(index);
	}

	@Override
	public int getWidgetCount() {
		return taperedBox.getWidgetCount();
	}

	@Override
	public int getWidgetIndex(Widget child) {
		return taperedBox.getWidgetIndex(child);
	}

	@Override
	public boolean remove(int index) {
		return taperedBox.remove(index);
	}

	@Override
	public void clear() {
		taperedBox.clear();
	}

	@Override
	public Iterator<Widget> iterator() {
		return taperedBox.iterator();
	}

	@Override
	public boolean remove(Widget w) {
		return taperedBox.remove(w);
	}

	@Override
	public void add(Widget w) {
		taperedBox.add(w);
	}

	@Override
	public void insert(Widget w, int beforeIndex) {
		taperedBox.insert(w, beforeIndex);
	}
}
